$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + "sys/incoming/list",
        datatype: "json",
        colModel: [
            {label: '进件id', name: 'incomingId', width: 40, align: 'center', sortable: false},
            {label: '标题', name: 'title', width: 95, align: 'center', sortable: false},
            {label: '跳转地址', name: 'jumpUrl', width: 90, align: 'center', sortable: false},
            {label: '封面图', name: 'bannerUrl', width: 45, align: 'center', sortable: false},
            {label: '创建时间', name: 'createTime', width: 75, align: 'center', sortable: false},
            {
                label: '是否显示', name: 'delFlag', width: 40, align: 'center', formatter: function (value, options, row) {
                    return value == 1 ? "是" : "否";
                }, sortable: false
            }
        ],
        viewrecords: true,
        height: 375,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        multiboxonly: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });


});


var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {},
        terminalTypes: [],
        showList: true,
        showUpdate: false,
        title: null,
        incoming: {
            incomingId: null,
            title: null,
            jumpUrl: null,
            bannerUrl: null,
            delFlag: 0
        }

    },
    methods: {
        query: function () {
            vm.reload();
        },
        clean: function () {
            vm.showList = true;
            vm.q = {};

            $("#jqGrid").jqGrid('setGridParam', {page: 1});
            vm.reload();
        },
        reload: function () {
            vm.showList = true;
            vm.showUpdate = false;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');

            $("#jqGrid").jqGrid('setGridParam', {
                postData: {},
                page: page
            }).trigger("reloadGrid");
        },
        deleteMer: function () {
            let rowid = getSelectedRow();
            if (rowid == null) {
                return;
            }
            let row = $("#jqGrid").jqGrid('getRowData', rowid);
            if (row == null) {
                return;
            }
            confirm("确定要删除该进件信息么？", function () {
                $.getJSON(baseURL + "sys/incoming/delete/" + row.incomingId, function (r) {
                    vm.reload();

                })

            })

        },
        toUpdate: function () {
            let rowid = getSelectedRow();
            if (rowid == null) {
                return;
            }
            let row = $("#jqGrid").jqGrid('getRowData', rowid);
            if (row == null) {
                return;
            }
            $.get(baseURL + "sys/incoming/getById/" + row.incomingId, r => {
                vm.incoming = r.incoming
            });
            vm.showList = false;
            vm.showUpdate = true;
            vm.title = "进件"
        },
        updateMer: function () {
            $.ajax({
                url: baseURL + "sys/incoming/update",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(vm.incoming),
                success: function (r) {
                    if (r.code == 0) {
                        alert('操作成功');
                        vm.reload();
                    } else {
                        alert(r.msg);
                    }
                }
            })
        }, modify() {
            let incomingBanner = $("#incomingBanner").val();
            if (incomingBanner == null || incomingBanner == '') {
                vm.updateMer();
            } else {
                var formData = new FormData($("form")[1]);
                formData.append("file", $("#incomingBanner")[0]);
                $.ajax({
                    url: baseURL + "sys/incoming/upload",
                    datatype: 'json',
                    type: 'post',
                    data: formData,
                    cache: false,
                    traditional: true,
                    contentType: false,
                    processData: false,
                    success: function (msg) {
                        if (msg.url != null && msg.url != '') {
                            vm.incoming.bannerUrl = msg.url;
                            vm.updateMer();
                        }
                    }
                })
            }


        }, saveorupdate() {
            if (vm.incoming.incomingId == null) {
                vm.addIncoming();
            } else {
                vm.modify();
            }


        },addIncoming(){
            let incomingBanner = $("#incomingBanner").val();
            if (incomingBanner == null || incomingBanner == '') {
                layer.msg("请选择封面图")
               return;
            }

            var formData = new FormData($("form")[1]);
            formData.append("file", $("#incomingBanner")[0]);
            $.ajax({
                url: baseURL + "sys/incoming/upload",
                datatype: 'json',
                type: 'post',
                data: formData,
                cache: false,
                traditional: true,
                contentType: false,
                processData: false,
                success: function (msg) {
                    if (msg.url != null && msg.url != '') {
                        vm.incoming.bannerUrl = msg.url;
                        $.ajax({
                            url: baseURL + "sys/incoming/save",
                            type: "post",
                            contentType: "application/json",
                            data: JSON.stringify(vm.incoming),
                            success: function (r) {
                                if (r.code == 0) {
                                    alert('操作成功');
                                    vm.reload();
                                } else {
                                    alert(r.msg);
                                }
                            }
                        })
                    }
                }
            })


        },add(){
            vm.showList = false;
            vm.showUpdate = true;
            vm.title = "进件"
            vm.incoming= {
                incomingId: null,
                    title: null,
                    jumpUrl: null,
                    bannerUrl: null,
                    delFlag: 0
            }

        }


    }
});


Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

